import pkg/heinepkg/lookup {.all.}
import pkg/matext
import std/strformat
import std/strutils

import std/sugar

import std/unicode

const separator = ";=== CONTENT BELOW IS AUTO-GENERATED ===;\n"
const highlightsFile = "queries/heine/highlights.scm"
const concealTableFile = "lua/heine/conceal.lua"
let before = readFile(highlightsFile).split(separator)[0]

var concealTable = "return {\n"

proc generateQueries(symbols: seq[(string, string)], highlight: string, conceal = true): string =
  let list = collect:
    for (heine, latex) in symbols:
      try:
        let text = matext(latex)
        if conceal and text.runeLen == 1 and text != heine:
          concealTable.add(&"  [\"{heine}\"] = \"{text}\",\n")
      except ValueError:
        discard
      escape(heine)
  fmt"""
((symbol) @{highlight}
  (#any-of? @{highlight} {list.join(" ")})
  (#heine-conceal! @{highlight}))
"""

let generated =
  generateQueries(latinLetters, "variable", conceal = false) &
  generateQueries(greekLetters & otherLetters, "variable") &
  generateQueries(mathbbLetters, "type") &
  generateQueries(wordOperators, "function") &
  generateQueries(logicOperators & relationOperators & bigOperators & otherOperators & punctuation, "operator")
concealTable.add("}\n")

writeFile(highlightsFile, before & separator & generated)
writeFile(concealTableFile, concealTable)
